package com.ssmTest.controller;


import com.ssmTest.model.User;
import com.ssmTest.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class UserController {
    @Resource
    private IUserService userService;

    @RequestMapping("/showUser.do")
    public void selectUser(HttpServletRequest request, HttpServletResponse response) throws IOException {

    }

    //返回审核列表
    @RequestMapping("/auditUser.do")
    public String selectUser(HttpSession session, ModelMap modelMap) {
        List<User> list = null;
        list=userService.selectByState(1);
        modelMap.addAttribute("list",list);
        return "auditUser";
    }

    //返回已经审核审核列表
    @RequestMapping("/auditList.do")
    public String auditList(HttpSession session, ModelMap modelMap) {
        List<User> list = userService.selectByStates();
        modelMap.addAttribute("list",list);
        return "auditList";
    }

    //返回审核列表
    @RequestMapping("/auditAgentUser.do")
    public String selectAgentUser(HttpSession session, ModelMap modelMap) {
        List<User> list = null;
        list=userService.selectByState(1);
        modelMap.addAttribute("list",list);
        return "auditAgentUser";
    }

    //返回审核列表
    @RequestMapping("/auditMachineUser.do")
    public String selectMachineUser(HttpSession session, ModelMap modelMap) {
        List<User> list = null;
        list=userService.selectByState(1);
        modelMap.addAttribute("list",list);
        return "auditMachineUser";
    }

    //审核成功
    @RequestMapping("/auditSuccess.do")
    public String selectsuccessUser(HttpSession session, int id,ModelMap modelMap) {
        User user=userService.selectUser(id);
        user.setState(2);
        userService.updateUserState(user);
        List<User> list = null;
        list=userService.selectByState(1);
        modelMap.addAttribute("list",list);
        return "auditUser";
    }

    //弹出界面
    @RequestMapping("/writeReasionPage.do")
    public  String writeReasionPage(ModelMap modelMap,int id,HttpSession session){
        session.setAttribute("uid",id);
        modelMap.addAttribute("id",id);
        modelMap.addAttribute("url","auditFail.do");
        return "writeReasionPage";
    }

    //审核失败
    @RequestMapping("/auditFail.do")
    public String selectfailUser(HttpSession session, String reasion) {
        System.out.println(reasion+"  "+session.getAttribute("uid"));
        User user=userService.selectUser(Integer.parseInt(session.getAttribute("uid").toString()));
        user.setFail_reasion(reasion);
        user.setState(3);
        userService.updateFailReasion(user);
        return "success";
    }
}
